GameBoy coding tutorial

deffy / confine

This article will cover the basics of Game Boy coding. In this issue only black/white graphics are covered, but there will probably (I don't promise) be a tutorial for the Game Boy Color in the next issue. However, by then you should have figured it out yourself. :)

First of all, why I wrote this article is because Adok wanted me to. :) Hehe.. no, but for all you people not knowing asm but still wanting to be able to develop games or demos with just the power of C. :) I assume you already have a GameBoy Color emulator (like GB98 or NoCash).

Ok.. First you need to get hold of a GameBoy C Compiler. The best one I have found is LCC. You should extract it to c:\SDK and set up some enviroment variables to "C:\SDK\gbz80-gb\2-1-0\bin" where the compiler is located.

Ok. You got your compiler. Now we will create our first gameboy program using C!

#include <gb.h>
#include <drawing.h>


void main()
{


	printf("This is my first gameboy program\n");
	printf("\nPress Start!");
	waitpad(J_START); // Other keys are J_A, J_UP, J_SELECT, etc.
	cls();

	color(BLACK, WHITE, SOLID);
	plot_point(GRAPHICS_WIDTH/2,   GRAPHICS_HEIGHT/2); 

	color(DKGREY, WHITE, SOLID);
 	plot_point(GRAPHICS_WIDTH/2+1, GRAPHICS_HEIGHT/2);

	color(LTGREY, WHITE, SOLID);
	plot_point(GRAPHICS_WIDTH/2+2, GRAPHICS_HEIGHT/2);

	color(WHITE, WHITE, SOLID);
	plot_point(GRAPHICS_WIDTH/2+3, GRAPHICS_HEIGHT/2);
}

This example will plot 4 pixels next to each other in the middle of the screen in the 4 different colors that exist in the Black And White gameboy.

color(BLACK; WHITE, SOLID) sets the drawing mode. The first parameter sets the foreground color. The second parameter sets the background color. The last parameter, "SOLID", is the drawing mode. SOLID will overwrite the existing pixel. There are also OR, XOR, and AND.

OK. Now let's say you want to create a box or a circle..

box(UBYTE x1, UBYTE y1, UBYTE x2, UBYTE y2, UBYTE style);

The last parameter is the style of the box. If you want the box to be filled your last parameter is M_FILL, and if you don't want it to be filled then you last parameter is M_NOFILL. Same goes for circles:

circle(UBYTE x, UBYTE y, UBYTE radius, UBYTE style);

Other functions....

line(UBYTE x1, UBYTE y1, UBYTE x2, UBYTE y2); //draws a line in the current drawing mode
                                                and color
getpix(UBYTE x, UBYTE y); //returns the current color of the pixel at (x,y)
wrtchr(char chr); //writes the character 'chr' in the defualt font at current position
gotogxy(UBYTE x, UBYTE y); // Sets the current text position at (x,y)
draw_image(unsigned char *data); // no comments

And last..

lcc -o test.gb test.c //will compile and link your program

This is my first tutorial so don't be mad at me if the code sucks or if the whole tutorial sucks. :) And I also want to tell GammaW that it is possible to do nice things with C in gameboy. I have seen it.

Mail me at: deffy@mucky.net
Or visit my page: http://www.deffy.com or http://www.deffy.cjb.net
Don't forget to visit my group's page: http://www.confine.org or http://www.confine.cjb.net

Ted Östrem
deffy / confine